package cn.kill.orderservice.controller;

import cn.kill.orderservice.pojo.PayOrder;
import cn.kill.orderservice.service.order.OrderService;
import cn.kill.orderservice.utils.ApiResponse;
import cn.kill.orderservice.utils.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 张在成
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService  orderService;

    @RequestMapping("/first")
    public  String  showFirst(Model model){
        return "WEB-INF/jsp/index";
    }


    @RequestMapping(value = "/saveOrder")
    @ResponseBody
    public JSONObject saveOrder(PayOrder order){
        //执行保存订单操作
        PayOrder payOrder=orderService.saveOrder(order.getBody(),order.getMoney());
        if (payOrder==null){
            return ApiResponse.failPay("保存失败");
        }
        return  ApiResponse.sucessPay("订单保存成功",payOrder);
    }

    @RequestMapping(value = "/selectOrder")
    @ResponseBody
    public Result selectOrderById(String orderNo){
        //执行订单查询
        PayOrder payOrder=orderService.selectOrderDetail(orderNo);
        if (payOrder==null){
            return  new Result(400,"订单为空",null);
        }
        return  new Result(200,"订单查询成功", payOrder);
    }


}
